package com.bupt.smart.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Created by HH on 2016/7/5.
 */
public class JsonHelper {
    //JavaBean => Map
    public static Map<String,Object> Bean2Map(Object javabean) {
        Map<String,Object> map = new HashMap<String,Object>();
        Method[] methods = javabean.getClass().getMethods();
        String key = null;
        Object value = null;
        String methodName = null;
        for(Method method:methods){
            methodName = method.getName();
            if(methodName.contains("get")){
                key = methodName.substring(3);
                try {
                    Object[] args = null;
                    value = method.invoke(javabean,args);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                map.put(key,value);
            }
        }
        return map;
    }
}
